class Scope:
    # 重载运算符
    allow_api = []
    allow_redprint = []
    forbidden_api = []

    def __add__(self, other):
        self.allow_api = list(set(self.allow_api + other.allow_api))
        self.allow_redprint = list(set(self.allow_redprint + other.allow_redprint))
        self.forbidden_api = list(set(self.forbidden_api + other.forbidden_api))
        return self


class SuperScope(Scope):
    allow_api = ['v1.a']

    def __init__(self):
        self + UserScope() + AdminScope()


class AdminScope(Scope):
    allow_api = ['v1.b']  # 允许哪样一些endpoint对应的视图函数进行调用
    allow_redprint = ['v1.user']

    def __init__(self):
        self + UserScope()


class UserScope(Scope):
    allow_api = ['v1.c']
    forbidden_api = ['v1.user+super_get_user', 'v1.user+super_del_user']
    allow_redprint = ['v1.user', 'v1.gift']


def is_in_scope(scope, endpoint):
    # 重要方法
    # user.auth  ---  Scope
    # Scope   ---- 是否有某个特定的视图函数的权限 endpoint
    gl = globals()
    scope = gl[scope]()  # 反射
    if endpoint in scope.forbidden_api:
        return False

    if endpoint in scope.allow_api:
        return True

    # v1.user+super_get_user
    splits = endpoint.split('+')
    redprint = splits[0]
    if redprint in scope.allow_redprint:
        return True
    else:
        return False

# if __name__ == '__main__':
#     print(is_in_scope('AdminScope', 'super_get_user'))  # True
#     print(is_in_scope('UserScope', 'super_get_user'))  # False

# a -- 超级管理员    A B C

# b -- 一般的管理员  B  C  所有用户模块的权限 用户模块user有100个视图函数

# c --  普通用户     C集合 用户模块user一共有100个视图函数，
# 只有两个管理员有的权限没有，这个时候就需要排除了


# u = UserScope()
# print(u.allow_api)
#
# a = AdminScope()
# print(a.allow_api)
#
# s = SuperScope()
# print(s.allow_api)
